//
//  MYY_FansViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/10/25.
//  Copyright © 2017年 hbjApple. All rights reserved.
// 我的粉丝

#import "MYY_FansViewController.h"
#import "MYYMeVisitorCell.h"
#import "MYY_VisitiorHeaderView.h"
#import "MYY_Me_Macro.h"
#import "myfollowListModel.h"
#import "NIMSessionListViewController.h"
#import "MYXNIMSessionViewController.h"
#import "myfanslistModel.h"
#import "MYCIndexCardViewController.h"

@interface MYY_FansViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
@property(nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property(nonatomic, strong) MYXHttpManager *MYXHttpManag;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger size;

@end

@implementation MYY_FansViewController
-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    
    return _dataArr;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.MYXHttpManag =[[MYXHttpManager alloc]init];
    [self.tableView.mj_header beginRefreshing];
    [self createUI];
    self.page = 1;
    self.size = 10;
}
#pragma mark - UI

- (void)createUI
{
    
    self.tableView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self.view addSubview:self.tableView];
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        WS(weakSelf)
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf loadNewData];
            
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf loadMorewData];
        }];
        
    }
    return _tableView;
}

#pragma mark - request

- (void)loadNewData
{
    self.tableView.hidden = NO;
    [self.tableView.mj_header beginRefreshing];
    [self.dataArr removeAllObjects];
   // self.pageStart = 1;
    if ([self.navigationItem.title isEqualToString:@"我的关注"]) {
         [self followListRequest];
    }else{
        [self myfansRequest];
    }
  
    
}

- (void)loadMorewData
{
    self.page ++;
    if (self.tableView.mj_header.isRefreshing) {
        return;
    }
   
}

#pragma mark - tableview datasource and delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MYYMeVisitorCell *MYYMeVisitorCe = [MYYMeVisitorCell cellWithTableView:tableView];
    MYYMeVisitorCe.backgroundColor =[UIColor whiteColor];
    myfollowListModel *myfollowListMod =self.dataArr[indexPath.row];
    myfollowListMod.titile =self.navigationItem.title;
    
   // myfollowListMod.image= [NSString stringWithFormat:@"%@%@",MYX_IMAGES_BaseUrl,myfollowListMod.image];
    MYYMeVisitorCe.myfollowListMode =myfollowListMod;
    MYYMeVisitorCe.ziliaoGes = ^{
        MYCIndexCardViewController *MYCIndexCardVC =[[MYCIndexCardViewController alloc]init];
        MYCIndexCardVC.navigationItem.title = myfollowListMod.nickname;
        MYCIndexCardVC.userID =myfollowListMod.follow_id;
        [self.navigationController pushViewController:MYCIndexCardVC animated:YES];
     };
    return MYYMeVisitorCe;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return MYY_CELL_MeVisitor;
}

/*
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
 forRowAtIndexPath:(NSIndexPath *)indexPath

{
    NSLog(@"shanchu");
    
}*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    myfollowListModel *myfollowListMod;
    if ([self.navigationItem.title isEqualToString:@"我的关注"]) {
        myfollowListMod =self.dataArr[indexPath.row];
        MYCIndexCardViewController *MYCIndexCardVC =[[MYCIndexCardViewController alloc]init];
        MYCIndexCardVC.userID =myfollowListMod.follow_id;
        MYCIndexCardVC.navigationItem.title = myfollowListMod.nickname;
        [self.navigationController pushViewController:MYCIndexCardVC animated:YES];
    }else{
       myfollowListMod =self.dataArr[indexPath.row];
    }
    
   
    /*
    if ([self.navigationItem.title isEqualToString:@"我的关注"]) {
        myfollowListModel *myfollowListMod =self.dataArr[indexPath.row];
        if ([myfollowListMod.is_follow isEqualToString:@"1"]) {
            NIMSession *session = [NIMSession session:[NSString stringWithFormat:@"abc%@",myfollowListMod.follow_id] type:NIMSessionTypeP2P];
            MYXNIMSessionViewController *vc = [[MYXNIMSessionViewController alloc] initWithSession:session];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [JXTHUDHelper showHUDText:@"没有相互关注，不能聊天哦～" type:@"error" inView:nil];
        }
        
    }else{
        myfollowListModel *myfollowListMod =self.dataArr[indexPath.row];
        if ([myfollowListMod.is_follow isEqualToString:@"0"]) {
            [self initAddfollow:myfollowListMod.user_id];
            
        }
    }*/
   
   
}
//MYCIndexCard ViewController
#pragma mark - 关注列表


-(void)followListRequest{
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    NSString *size = [NSString stringWithFormat:@"%ld",self.size];
    [self.MYXHttpManag  myfollowList:^(NSArray *followListArray) {
        if (followListArray.count > 0) {
            [self.dataArr addObjectsFromArray:followListArray];
            [self.tableView.mj_footer endRefreshing];
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSString *errorCode) {
        [self showServerErrorOfFeedList];
    } netDown:^{
        [self showNetDownOfFeedList];
    } page:page
      size:size];
}

#pragma mark - 粉丝列表


-(void)myfansRequest{
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    NSString *size = [NSString stringWithFormat:@"%ld",self.size];    [self.MYXHttpManag  myfansList:^(NSArray *followListArray) {
        if (followListArray.count > 0) {
            [self.dataArr addObjectsFromArray:followListArray];
            [self.tableView.mj_footer endRefreshing];
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSString *errorCode) {
        [self showServerErrorOfFeedList];
    } netDown:^{
        [self showNetDownOfFeedList];
    } page:page
      size:size];
}

- (void)showNetDownOfFeedList
{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    [self showTopMessage:@"当前网络不可用, 请检查网络设置"];
}

- (void)showServerErrorOfFeedList
{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    [self showTopMessage:@"服务异常, 请稍后重试"];
}


#pragma mark - 加关注


-(void)initAddfollow:(NSString *)userID{
    
    [self.MYXHttpManag addfollow:userID success:^(NSString *dataStr) {
        [self showTopMessage:@"成功加入关注"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"info" object:nil];
        [self myfansRequest];
    } failure:^(NSString *errorCode) {
         [self showServerErrorOfFeedList];
    } netDown:^{
        [self showNetDownOfFeedList];
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
